package com.googlecode.jsimplemarket.security;

import com.googlecode.jsimplemarket.model.Utilizator;
import java.util.Collection;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.wicketstuff.security.hive.authentication.DefaultSubject;
import org.wicketstuff.security.hive.authorization.SimplePrincipal;

/**
 *
 * @author alex
 */
public class SpringSubject extends DefaultSubject {
    private static final long serialVersionUID = 1L;

    private final String username;
    private final Utilizator utilizator;

    public SpringSubject(Authentication springSecurityAuthentication) {
        super();

        this.username = springSecurityAuthentication.getName();
        this.utilizator = (Utilizator) springSecurityAuthentication.getPrincipal();

        Collection<? extends GrantedAuthority> authorities = springSecurityAuthentication.getAuthorities();
        if (authorities != null) {
            for (GrantedAuthority authority : authorities) {
                addPrincipal(new SimplePrincipal(authority.getAuthority()));
            }
        }
    }

    public String getUsername() {
        return this.username;
    }

    public Utilizator getUtilizator() {
        return utilizator;
    }
}
